package com.spark.bitrade.controller.cms;

import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.spark.bitrade.constant.BooleanEnum;
import com.spark.bitrade.constant.PageModel;
import com.spark.bitrade.controller.common.BaseAdminController;
import com.spark.bitrade.entity.Feedback;
import com.spark.bitrade.entity.QFeedback;
import com.spark.bitrade.service.FeedbackService;
import com.spark.bitrade.util.MessageResult;
import com.spark.bitrade.util.PredicateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther Cain
 * @date 2018/12/20
 * @time 16:04
 */
@RestController
@RequestMapping("/cms/feedback")
public class FeedBackController extends BaseAdminController {
    @Autowired
    private FeedbackService feedbackService;

    @RequiresPermissions("cms:feedback:manager")
    @PostMapping("list")
    public MessageResult list(PageModel pageModel, @RequestParam(value = "alreadyRead",required = false)BooleanEnum alreadyRead){
        List<BooleanExpression> booleanExpressionList=new ArrayList<>();
        if(alreadyRead!=null){
            booleanExpressionList.add(QFeedback.feedback.alreadyRead.eq(alreadyRead));
        }
        Predicate predicate= PredicateUtils.getPredicate(booleanExpressionList);
        Page<Feedback> feedbackPage=feedbackService.findAll(predicate,pageModel);
        MessageResult result=MessageResult.success();
        result.setData(feedbackPage);
        return result;
    }

    @RequiresPermissions("cms:feedback:manager")
    @PostMapping("detail")
    public MessageResult detail(@RequestParam("id")Long id){
        Feedback feedback=feedbackService.findById(id);
        feedback.setAlreadyRead(BooleanEnum.IS_TRUE);
        feedbackService.save(feedback);
        MessageResult result=MessageResult.success();
        result.setData(feedback);
        return result;
    }
}
